﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Solution.Application.Dtos;
using Solution.Application.Services;

namespace Solution.Application.Workers
{
    public class KafkaPublisher : BackgroundService
    {
        private readonly IKafkaService _kafkaService;

        public KafkaPublisher(IKafkaService kafkaService)
        {
            _kafkaService = kafkaService;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Console.WriteLine("start send message to kafka service .. ");
            for (int i = 0; i < 10; i++)
            {
                await CreateOrderAsync("test-topic");
            }
        }

        private async Task CreateOrderAsync(string topicName)
        {
            var request = new EventInput<CreateOrderInput>()
            {
                Data = new CreateOrderInput()
                {
                    ProductName = Guid.NewGuid().ToString(),
                    UserId = 0,
                    CreateOrderTime = DateTime.UtcNow,
                }
            };

            await _kafkaService.ProducerAsync(topicName, request);
        }
    }
}